package com.fxust.dialog;

/**
 * @auto johnking
 * @create 2017-12-17 上午11:35
 **/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.fxust.thread.DealTextFileThread;
import org.apache.commons.lang3.StringUtils;

public class DealTextFileDialog extends BaseDialog {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -4470516487826379358L;

    private String uuid;
    private final JPanel contentPanel = new JPanel();

    private JButton dealTextFileButton = null;

    /** 文件路径浏览Button */
    private JButton browseButton = null;

    private JTextField filepathTextField = null;

    public DealTextFileDialog(Frame frame, String title, String uuid) {
        super(frame, title);
        this.uuid = uuid;

        setBounds(100, 100, 460, 300);
        getContentPane().setLayout(new BorderLayout());

        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setLayout(null);
        getContentPane().add(contentPanel, BorderLayout.CENTER);

        {
            JLabel filepathLabel = new JLabel("文件路径：");
            filepathLabel.setBounds(10, 45, 65, 15);
            filepathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            contentPanel.add(filepathLabel);

            // 文件路径TextField
            filepathTextField = new JTextField();
            filepathTextField.setBounds(80, 42, 201, 21);
            filepathTextField.setColumns(10);
            contentPanel.add(filepathTextField);

            // 浏览Button
            browseButton = new JButton("浏览");
            browseButton.setBounds(280, 41, 60, 23);
            contentPanel.add(browseButton);

            // 开始查询Button
            dealTextFileButton = new JButton("开始处理");
            dealTextFileButton.setBounds(350, 41, 90, 23);
            contentPanel.add(dealTextFileButton);
        }
        {
            JLabel filepathLabel = new JLabel("处理进度：");
            filepathLabel.setBounds(10, 80, 65, 21);
            filepathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            contentPanel.add(filepathLabel);
            // 详情进度TextField
            dealNumTextField = new JTextField();
            dealNumTextField.setEditable(false);
            dealNumTextField.setColumns(10);
            dealNumTextField.setBounds(80, 80, 110, 21);
            contentPanel.add(dealNumTextField);

            // 详情进度ProgressBar
            dealProgressBar = new JProgressBar();
            dealProgressBar.setMaximum(100);
            dealProgressBar.setBounds(200, 80, 240, 21);
            contentPanel.add(dealProgressBar);
        }

        addListener();

    }

    private void addListener() {
        // [路径选择]的点击事件
        browseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                browseFilePathClicked();
            }
        });
        // [开始下载Button]的点击事件
        dealTextFileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startDealTextFileMain();
            }
        });

    }

    private void startDealTextFileMain() {
        // 验证界面值得有效性
        String filePath = filepathTextField.getText();
        if (!varifyFilePath(filePath)) {
            return;
        }
        // 启动线程
        new DealTextFileThread(uuid,filePath).start();
    }
    /**
     * 文件选择校验
     * @author fgq 2017年11月24日 上午10:01:38
     * @param filePath
     * @return
     */
    protected boolean varifyFilePath(String filePath) {
        // 验证文件夹路径是否有效
        if (StringUtils.isBlank(filePath)) {
            JOptionPane.showMessageDialog(null, "文件不能为空，请重新选择文件", "错误信息", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        File folder = new File(filePath);
        if (!folder.isFile()) {
            JOptionPane.showMessageDialog(null, "非文件类型,请重新选择", "错误信息", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    /**
     * 浏览文件路径
     * @author fgq 2017年8月8日 下午3:26:25
     */
    private void browseFilePathClicked() {
        String fileName = openFileChooser(JFileChooser.FILES_ONLY, null, null);
        if (StringUtils.isNotBlank(fileName)) {
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if(!"txt".equalsIgnoreCase(suffix)){
                JOptionPane.showMessageDialog(null, "仅支持TXT类型文件,请重新选择", "错误信息", JOptionPane.ERROR_MESSAGE);
                return;
            }
            filepathTextField.setText(fileName);
        }
    }
}
